$(function(){
    var adAdd = new AdAdd();
    adAdd.init().init();
});

function AdAdd(){
    this.init = function(){
        return ADADD;
    };
    
    var ADADD = {
        init : function(){
            ADADD.funs.initUpload();
            ADADD.lieteners.bindEvents();
        },
        lieteners : {
            bindEvents : function(){
                $.util.foreachFunction(this, [ "bindEvents" ]);
            },
            bindFormSubmit : function(){
                $("#btn_notice_add_submit").click(function(){
                    var $form = $("#frm_ad_add");
                    
                    $form.validate();
                    
                    var icon = $.trim($("#hid_img_url").val());
                    if('' == icon){
                        alert('未上传广告图片');
                        
                        return;
                    }
                    
                    $form.submit();
                });
            },
            bindUpload : function(){
                $("#inp_ad_upload_enter").click(function(){
                    $('#inp_ad_upload').uploadifyUpload();
                });
            },
            bindShowImg : function(){
                //$("a.preview").preview();
            },
            bindDelete : function(){
                $("#table_agents_manager a.c_ad_delete").click(function(){
                    if(confirm('是否确定删除')){
                        $.util.initHiddenFormSubmit($('#frm_hidden_app_index'),SYSTEM_DATA.contextPath + '/Ad/adDel.shtml',  [{
                            'name' : 'id',
                            'value' : $(this).attr('ad_id')
                        }]);
                    }
                });
            }
        },
        funs :{
            initUpload : function(){
                $('#inp_ad_upload').uploadify({
                    'uploader'  : SYSTEM_DATA.public_path + '/js/plugin/Uploadify/uploadify.swf',
                    'script'    : SYSTEM_DATA.contextPath + '/File/upload.shtml',
                    'cancelImg' : SYSTEM_DATA.public_path + '/js/plugin/Uploadify/cancel.png',
                    'folder'    : '/uploads',
                    'removeCompleted' : true,
                    'auto'       : false,
                    'fileExt' : '*.jpg;*.jpeg;*.png;*.gif',
                    'fileDesc' : '可上传的图片文件',
                    'sizeLimit' : 1024 * 1024 * 2,
                    //'height' : 18,
                    //'buttonText' : '上传',
                    'onComplete'  : function(event, ID, fileObj, response, data) {
                        //console.debug(response);
                        response = $.parseJSON(response);
                        //console.debug(response);
                        
                        if(1 == response.status){
                            $('#hid_img_url').val(response.data.path);
                        }else{
                            alert(response.info);
                        }
                    },
                    'onError' : function(event,ID,fileObj,errorObj) {
                        console.debug(errorObj);
                    }
                });
            }
            
            
        }
    };
}